package day63;

import static org.junit.Assert.assertNotNull;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class IOTest {

	@Test
	public void testFileReader() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("io.txt");
			//FileReader fileReader = new FileReader("src/day63/1.txt");
			//FileReader fileReader = new FileReader("D://2.txt");
			int ch1 = fileReader.read();
			System.out.println(ch1);// 97
			System.out.println((char)ch1);// a
			int ch2 = fileReader.read();
			System.out.println(ch2);// 98
			System.out.println((char)ch2);// b
			int ch3 = fileReader.read();
			System.out.println(ch3);// 99
			System.out.println((char)ch3);// c
			
			// 如果已到达流的末尾，则返回 -1
			int ch4 = fileReader.read();
			System.out.println(ch4);// -1
			
			// 编译时异常，未雨绸缪，这个文件可能会不存在
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFileReader2() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("io.txt");
			int ch = -1;
			// while循环去读，直到ch的值等于-1，就退出循环
			while ((ch = fileReader.read()) != -1) {
				System.out.print((char)ch);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFileReader3() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("io.txt");
			// public int read(char[] cbuf)   buffer
			// 将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前，此方法一直阻塞。 
			// 返回值：读取的字符数，如果已到达流的末尾，则返回 -1 
			//char[] buffer = new char[1024];
			char[] buffer = new char[20];
			// eirherhehreirhie34dg
			// eirherhehreirhie34dg
			// dff

			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println(length);
				System.out.println(buffer);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFileReader4() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("io.txt");
			fileWriter = new FileWriter("io_back.txt");
			char[] buffer = new char[20];
			int length = 0;
			// public abstract void write(char[] cbuf,int off, int len)
//			参数：
//			cbuf - 字符数组
//			off - 开始写入字符处的偏移量
//			len - 要写入的字符数 
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println(length);
				System.out.println(buffer);
				fileWriter.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testInputStream() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("a.jpg");
			fileOutputStream = new FileOutputStream("a_back.jpg");
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	@Test
	public void testObjectOutputStream() {
		Student student = new Student(1, "zhangsan", 23, "男");
		ObjectOutputStream objectOutputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("student");
			objectOutputStream = new ObjectOutputStream(fileOutputStream);
			objectOutputStream.writeObject(student);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testObjecInputStream() {
		FileInputStream fileInputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			fileInputStream = new FileInputStream("student");
			objectInputStream = new ObjectInputStream(fileInputStream);
			Student student = (Student)objectInputStream.readObject();
			System.out.println(student);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (objectInputStream != null) {
				try {
					objectInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void test22()  {
		try {
			FileInputStream fileInputStream = new FileInputStream("io.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
}
